package com.sunlands.deskmate.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author shixiaopeng
 */
@Component
public class RedisUtil {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 存储 历史消息
     * @param key
     * @param value
     * @param score
     */
    public void addZset(String key, Object value, double score){
        redisTemplate.opsForZSet().add(key, value, score);
        redisTemplate.expire(key, 1, TimeUnit.DAYS);
    }

    /**
     * 删除历史消息
     * @param key
     * @param min
     * @param max
     * @return
     */
    public Long removeRange(String key, double min, double max){
        return redisTemplate.opsForZSet().removeRangeByScore(key, min, max);
    }

    /**
     * 获取历史消息
     * @param key
     * @return
     */
    public Set<Object> rangeAll(String key){
        return redisTemplate.opsForZSet().range(key, 0 , -1);
    }

    /**
     * 存储在房间/私聊/群聊的用户
     * @param key
     * @param value
     */
    public void addOnlineSet(String key, Long... value){
        redisTemplate.opsForSet().add(key, value);
        redisTemplate.expire(key, 1, TimeUnit.DAYS);
    }

    /**
     * 获取在房间/私聊/群聊的用户
     * @param key
     * @return
     */
    public Set<Object> members(String key){
        return redisTemplate.opsForSet().members(key);
    }

    /**
     * 存储某个用户所在的私聊/群聊/房间的所有key
     * @param key
     * @param value
     */
    public void addKeySet(String key, String... value){
        redisTemplate.opsForSet().add(key,value);
        redisTemplate.expire(key, 1, TimeUnit.DAYS);
    }

    /**
     * 获取用户所在的私聊/群聊/房间的所有key
     * @param key
     * @return
     */
    public Set<Object> getKeySet(String key){
        return redisTemplate.opsForSet().members(key);
    }

    public void remove(String key, Long... value){
        redisTemplate.opsForSet().remove(key,value);
    }

    public void delete(String key){
        redisTemplate.delete(key);
    }
}
